﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Minio.DataModel;
using Minio;
using System.Security.AccessControl;
using System.Diagnostics;
using System.Net;
using Minio.Exceptions;
using System.Net.Mime;

namespace NETCORE.TMinio.Controllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class TextController : ControllerBase
	{
		public TextController()
		{
		}

		[HttpPost]
		[Route("MiniotAsync")]
		public async Task<IActionResult> MiniotAsync()
		{
			var minioClient = new MinioClient("172.16.40.177:9200", "LIUXINYU", "qI7-wW1]oO6");
			// 现在你可以使用 minioClient 来进行操作，例如列出所有存储桶
			try
			{
				var buckets = minioClient.ListBucketsAsync().Result;

				var BucketName = "sn-public";

				// 确保存储桶存在
				bool found = await minioClient.BucketExistsAsync(BucketName);
				if (!found)
				{
					await minioClient.MakeBucketAsync(BucketName);
					Console.WriteLine("Bucket {0} created successfully", BucketName);
				}

				var FilePath = Directory.GetCurrentDirectory() + "\\tt.txt";

				// 读取文件内容并上传
				using (var stream = System.IO.File.OpenRead(FilePath))
				{
					await minioClient.PutObjectAsync(BucketName, "tttttttttt", stream, stream.Length);
					Console.WriteLine("File uploaded successfully");
				}

			}
			catch (MinioException e)
			{
				Console.WriteLine("Error: {0}", e.Message);
			} 

			return Ok();
		}
	}
}
